package com.shm.controller;

import java.rmi.ServerException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.Follow;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.ErrorCode;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.shm.service.IFollowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 关注Controller
 *
 * @author dam
 * @date 2023-08-08
 */
@RestController
@RequestMapping("/market/follow")
public class FollowController extends BaseController {
    @Autowired
    private IFollowService followService;

    /**
     * 查询关注列表
     */
    @PreAuthorize("@ss.hasPermi('market:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(Follow follow) {
        startPage();
        List<Follow> list = followService.list(new QueryWrapper<Follow>(follow));
        return getDataTable(list);
    }

    /**
     * 查询是否已经关注了
     */
    @PreAuthorize("@ss.hasPermi('market:follow:list')")
    @GetMapping("/hadFollowSomeone/{followedId}")
    public AjaxResult hadFollowSomeone(@PathVariable("followedId") Long followedId) {
        QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<Follow>();
        followQueryWrapper.eq("follower_id", getLoginUser().getUserId());
        followQueryWrapper.eq("followed_id", followedId);
        Follow one = followService.getOne(followQueryWrapper);
        return AjaxResult.success().put("hadFollow", one != null ? true : false);
    }

    /**
     * 导出关注列表
     */
    @PreAuthorize("@ss.hasPermi('market:follow:export')")
    @Log(title = "关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Follow follow) {
        List<Follow> list = followService.list(new QueryWrapper<Follow>(follow));
        ExcelUtil<Follow> util = new ExcelUtil<Follow>(Follow.class);
        util.exportExcel(response, list, "关注数据");
    }

    /**
     * 获取关注详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:follow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(followService.getById(id));
    }

    /**
     * 新增关注
     */
    @PreAuthorize("@ss.hasPermi('market:follow:add')")
    @Log(title = "关注", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Follow follow) {
        // 设置商品主人
        LoginUser loginUser = getLoginUser();
        if (loginUser.getUserId().equals(follow.getFollowedId())) {
            throw new ServiceException("自己不能关注自己", ErrorCode.OPERATION_ERROR.getCode());
        }
        follow.setFollowerId(loginUser.getUserId());
        // 判断是否已经关注过该用户
        long count = followService.count(new QueryWrapper<>(follow));
        if (count > 0) {
            throw new ServiceException("已经关注该用户", ErrorCode.OPERATION_ERROR.getCode());
        }

        return toAjax(followService.save(follow));
    }

    /**
     * 修改关注
     */
    @PreAuthorize("@ss.hasPermi('market:follow:edit')")
    @Log(title = "关注", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Follow follow) {
        return toAjax(followService.updateById(follow));
    }

    /**
     * 删除关注
     */
    @PreAuthorize("@ss.hasPermi('market:follow:remove')")
    @Log(title = "关注", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(followService.removeByIds(ids));
    }

    /**
     * 取消关注
     */
    @PreAuthorize("@ss.hasPermi('market:follow:remove')")
    @Log(title = "取消关注", businessType = BusinessType.DELETE)
    @GetMapping("/cancelFollowSomeone/{followedId}")
    public AjaxResult cancelFollowSomeone(@PathVariable("followedId") Long followedId) {
        QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<Follow>();
        followQueryWrapper.eq("follower_id", getLoginUser().getUserId());
        followQueryWrapper.eq("followed_id", followedId);
        boolean remove = followService.remove(followQueryWrapper);
        return AjaxResult.success();
    }
}
